import { login, list } from '@/api/user'
import Cookies from 'js-cookie'

const user = {
  state: {
    token: localStorage.getItem('token'),
    opCode: localStorage.getItem('opCode'),
    opName: localStorage.getItem('opName'),
    departId: localStorage.getItem('departId'),
    departName: localStorage.getItem('departName'),
    groups: JSON.parse(localStorage.getItem('groups')),
    roles: JSON.parse(localStorage.getItem('roles')),
    list: [],
    ws: null
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_OP_CODE: (state, opCode) => {
      state.opCode = opCode
    },
    SET_OP_NAME: (state, opName) => {
      state.opName = opName
    },
    SET_DEPART_ID: (state, departId) => {
      state.departId = departId
    },
    SET_DEPART_NAME: (state, departName) => {
      state.departName = departName
    },
    SET_GROUPS: (state, groups) => {
      state.groups = groups
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_USER_LIST: (state, list) => {
      state.list = list
    },
    SET_WS: (state, ws) => {
      state.ws = ws
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const loginUser = userInfo.loginUser.trim()
      return new Promise((resolve, reject) => {
        login(loginUser, userInfo.password).then(res => {
          const data = res.data
          localStorage.setItem('token', data.token)
          Cookies.set('Authorization', data.token)
          localStorage.setItem('opCode', data.opCode)
          localStorage.setItem('opName', data.opName)
          localStorage.setItem('departId', data.departId)
          localStorage.setItem('departName', data.departName)
          localStorage.setItem('roles', JSON.stringify(data.roles))
          localStorage.setItem('groups', JSON.stringify(data.groups))
          commit('SET_TOKEN', data.token)
          commit('SET_OP_CODE', data.opCode)
          commit('SET_OP_NAME', data.opName)
          commit('SET_DEPART_ID', data.departId)
          commit('SET_DEPART_NAME', data.departName)
          commit('SET_ROLES', data.roles)
          commit('SET_GROUPS', data.groups)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_OP_CODE', '')
        commit('SET_OP_NAME', '')
        commit('SET_DEPART_ID', '')
        commit('SET_DEPART_NAME', '')
        commit('SET_GROUPS', '')
        commit('SET_ROLES', [])
        // localStorage.removeItem('token')
        // localStorage.removeItem('opCode')
        // localStorage.removeItem('opName')
        // localStorage.removeItem('departId')
        // localStorage.removeItem('departName')
        // localStorage.removeItem('roles')
        // localStorage.removeItem('templates')
        localStorage.clear()
        commit('SET_WS', null)
        resolve()
      })
    },

    UserList({ state, commit }) {
      return new Promise((resolve, reject) => {
        if (state.list.length == 0) {
          list().then(res => {
            commit('SET_USER_LIST', res.data)
            resolve(res.data)
          }).catch(err => reject(err))
        } else {
          resolve(state.list)
        }
      })
    }
  }
}

export default user
